package com.baizw.cultivation.module.admin.controller;

import com.baizw.cultivation.common.annotation.NoRepeatSubmit;
import com.baizw.cultivation.common.page.CustomPage;
import com.baizw.cultivation.common.response.ResultBean;
import com.baizw.cultivation.module.admin.entity.RoleEntity;
import com.baizw.cultivation.module.admin.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 角色管理
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年05月26日 15:32
 */
@Valid
@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @NoRepeatSubmit
    @PostMapping("/list/page")
    @ApiOperation("分页查询角色列表")
    @RequiresPermissions("RoleList:select")
    public ResultBean listRole(@RequestBody CustomPage customPage) {
        CustomPage<RoleEntity> entityList = roleService.listRole(customPage);
        return ResultBean.ok().setResult(entityList);
    }

    @ApiOperation("新增/更新资源")
    @PostMapping("/save_or_update")
    @CacheEvict(cacheNames = {"role", "sys_role"}, allEntries = true)
    public ResultBean saveResource(@RequestBody RoleEntity roleEntity) {
        return ResultBean.isTrue(roleService.saveOrUpdate(roleEntity));
    }

    @ApiOperation("删除资源")
    @DeleteMapping("/delete/{roleIds}")
    @CacheEvict(cacheNames = {"role", "sys_role"}, allEntries = true)
    public ResultBean deleteById(@PathVariable Long[] roleIds) {
        return ResultBean.isTrue(roleService.removeByIds(Arrays.asList(roleIds)));
    }
}
